#include <QTimer>
#include "MainWindow.h"
#include "ui_MainWindow.h"
#include "Preferences.h"



MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    createTrayIcon();
    connect(TrayIcon, SIGNAL(activated(QSystemTrayIcon::ActivationReason)),this, SLOT(TrayIconActivated(QSystemTrayIcon::ActivationReason)));

    if(!set.getValueOfKey(SettingsKeys::KEY_MAINWINDOW_POS).toPoint().isNull())
        move(set.getValueOfKey(SettingsKeys::KEY_MAINWINDOW_POS).toPoint());
    else
    {
        QRect frect = frameGeometry();
        frect.moveCenter(QDesktopWidget().availableGeometry().center());
        move(frect.topLeft());
    }

    if(set.getValueOfKey(SettingsKeys::KEY_MAINWINDOW_SIZE).toSize().isValid())
        resize(set.getValueOfKey(SettingsKeys::KEY_MAINWINDOW_SIZE).toSize());


}

MainWindow::~MainWindow()
{
    delete ui;
    delete TrayIcon;
}

void MainWindow::CheckAccount()
{
    if(!set.contains(SettingsKeys::KEY_COUNT_ACCOUNT))
    {
        int n=0;
        set.setValueForKey(SettingsKeys::KEY_COUNT_ACCOUNT,n);
    }
    if(0 == set.getValueOfKey(SettingsKeys::KEY_COUNT_ACCOUNT).toInt())
        CreatingAccountWindow();
}

void MainWindow::CreatingAccountWindow()
{
    LoginDialog l;
    l.exec();
}

void MainWindow::closeEvent(QCloseEvent *event){

    set.setValueForKey(SettingsKeys::KEY_MAINWINDOW_POS,pos());
    set.setValueForKey(SettingsKeys::KEY_MAINWINDOW_SIZE,size());

}
void MainWindow::changeEvent(QEvent *event)
{
    if(event->type()==QEvent::WindowStateChange)
    {
        if(isMinimized())
        {
            if(TrayIcon)
            {
                QTimer::singleShot(0, this, SLOT(hide()));
                TrayIcon->show();

            }
        }
        else
        {
            if( isHidden() )
            {
                showNormal();
                TrayIcon->hide();
            }
        }
    }

    QMainWindow::changeEvent(event);
}

void MainWindow::TrayIconActivated(QSystemTrayIcon::ActivationReason reason)
{
    switch(reason)
    {
        case QSystemTrayIcon::Trigger:
        {
            break;
        }
        case QSystemTrayIcon::DoubleClick:
        {
            showNormal();
            setWindowState( windowState() & Qt::WindowActive );
            activateWindow();
            raise();
            TrayIcon->hide();
            break;
        }
        case QSystemTrayIcon::MiddleClick:
        {
            break;
        }
        default:
        {
            break;
        }
    }
}


void MainWindow::createTrayIcon()
{
    QAction *showMainWindow = new QAction(tr("&Show"), this);
    connect(showMainWindow,SIGNAL(triggered()),this,SLOT(showNormal()));

    //QAction *checkEmail = new QAction(tr("Check e&mail"), this);

    QAction *showPreferencesDialog = new QAction(tr("&Settings"),this);
    connect(showPreferencesDialog,SIGNAL(triggered()),this,SLOT(on_OptionButton_clicked()));

    QAction *quitAction = new QAction(tr("&Quit"),this);
    connect(quitAction,SIGNAL(triggered()),qApp,SLOT(quit()));

    TrayIconMenu = new QMenu(this);
    TrayIconMenu->addAction(showMainWindow);
    //TrayIconMenu->addAction(checkEmail);
    TrayIconMenu->addAction(showPreferencesDialog);
    TrayIconMenu->addSeparator();
    TrayIconMenu->addAction(quitAction);

    TrayIcon = new QSystemTrayIcon(this);
    TrayIcon->setContextMenu(TrayIconMenu);
    TrayIcon->setIcon(QIcon(":/image/trayicon.png"));
    setWindowIcon(QIcon(":/image/trayicon.png"));

}


void MainWindow::on_OptionButton_clicked()
{
    Preferences p;
    connect(&p,SIGNAL(CreateDatabase(QString)),this,SIGNAL(CreateDatabase(QString)));
    connect(&p,SIGNAL(DeleteDatabase(QString)),this,SIGNAL(DeleteDatabase(QString)));
    p.exec();

}

void MainWindow::on_NewMailButton_clicked()
{

}

void MainWindow::on_ReplyMailButton_clicked()
{

}

void MainWindow::on_TrashMailButton_clicked()
{

}

void MainWindow::on_SignOutButton_clicked()
{

}

void MainWindow::on_SearchButton_clicked()
{

}
